Repository navigation
fix: Treat ^ as exponentation in DuckDB - #2972
nuno-faria wants to merge 1 commit into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2972 +/- ##
=======================================
Coverage 83.28% 83.28%
=======================================
Files 42 42
Lines 35355 35355
Branches 35355 35355
=======================================
Hits 29446 29446
Misses 2458 2458
Partials 3451 3451 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
LucaCappelletti94
left a comment
There was a problem hiding this comment.
A few notes, besides in the ones in the code:
- Add the fuzzer seeds and actually run the fuzzers
- You should give
^its own precedence above*in DuckDB, so as to handle the precedence error illustrated in the test.
| // In PostgreSQL and DuckDB, ^ stands for the exponentiation operation. | ||
| // See https://www.postgresql.org/docs/current/functions-math.html, | ||
| // https://duckdb.org/docs/2.0/sql/functions/numeric. | ||
| if dialect_is!(dialect is PostgreSqlDialect | DuckDbDialect) { |
There was a problem hiding this comment.
Add a new method to the Dialect trait instead of extending the use of the dialect_is macro.
| #[test] | ||
| fn test_exponentiation() { | ||
| let expected_expr = Expr::BinaryOp { | ||
| left: Box::new(Expr::Identifier(Ident::new("a"))), | ||
| op: BinaryOperator::PGExp, | ||
| right: Box::new(Expr::Identifier(Ident::new("b"))), | ||
| }; | ||
| assert_eq!(duckdb().verified_expr("a ^ b"), expected_expr); | ||
| } |
There was a problem hiding this comment.
You should assert the trees that depend on precedence, because a ^ b alone passes while a * b ^ c parses as (a * b) ^ c.
| #[test] | |
| fn test_exponentiation() { | |
| let expected_expr = Expr::BinaryOp { | |
| left: Box::new(Expr::Identifier(Ident::new("a"))), | |
| op: BinaryOperator::PGExp, | |
| right: Box::new(Expr::Identifier(Ident::new("b"))), | |
| }; | |
| assert_eq!(duckdb().verified_expr("a ^ b"), expected_expr); | |
| } | |
| #[test] | |
| fn test_exponentiation() { | |
| use BinaryOperator::{Multiply, PGExp, Plus}; | |
| let ident = |name| Box::new(Expr::Identifier(Ident::new(name))); | |
| let binary = |left, op, right| Box::new(Expr::BinaryOp { left, op, right }); | |
| let minus = |expr| Box::new(Expr::UnaryOp { op: UnaryOperator::Minus, expr }); | |
| for (sql, expected) in [ | |
| ("a * b ^ c", binary(ident("a"), Multiply, binary(ident("b"), PGExp, ident("c")))), | |
| ("a + b ^ c", binary(ident("a"), Plus, binary(ident("b"), PGExp, ident("c")))), | |
| ("-a ^ b", binary(minus(ident("a")), PGExp, ident("b"))), | |
| ("a ^ b ^ c", binary(binary(ident("a"), PGExp, ident("b")), PGExp, ident("c"))), | |
| ] { | |
| assert_eq!(duckdb().verified_expr(sql), *expected, "{sql}"); | |
| } | |
| } |
|
Thanks @LucaCappelletti94.
I don't think there is a way to make the negation work here. I searched around and it appears it is already being worked on: #2829
This part is not clear to me. Is the fuzzer test to check the precedence bug? If so do we need to create a |
Dialects: DuckDB
Closes: #2971
Like Postgres,
^should be represented as exponentiation in DuckDB: https://duckdb.org/docs/2.0/sql/functions/numeric